PHP Core / OOPs / Oops Basics
Oops Basics
-
Note
1. What is oops
1. OOPS stands for Object oriented programming structure.
2. It helps the developer to think and organize the code in terms of real world objects
3. It also helps to design the code more modular, flexible, reusable and maintainable.
4. OOPs groups related data and functions into classes
2. Explain with a example
In hospital management, "hospital", "doctor", "patient", "treatment", and "medicines" are real world objects. Each object have its own properties and behaviors.
When developing a hospital management application, developer create classes for each real world objects and define the attributes and methods based on its properties and behaviors
3. What is the use of Oops OR why should use oops?
To design modular, flexible, reusable and maintainable code
4. OOPS features
1. ClassClass is a blueprint of real world objects. It has attributes (member variables) and methods (member functions)
2. ObjectObject is an instance of a class.
When creating a instance, memory is allocated for the attributes in the object
Each object has seperate memory location to store its own data
3. Inheritance (Code Reusability)Common code can be written in a base (parent) class and reused in child classes.
4. Polymorphism (Code maintainablity )You can use the same method name in parent and child classes with different behaviors.
5. Encapsulation ( Security)1. Hides internal data.
2. Keeps data safe from outside of the class using private and protected access modifiers .
3. Helps to prevent accidental changes.
6. AbstractionHides internal logic and shows only relevant details using abstract classes or interfaces.
Summary
1. Abstract class vs interfaceFeatures Usage code implementation Class To design properties and methods of an object keyword "class" Object To store data keyword "new" Inheritance Share the properties and logic to child class keyword "extends" Encapsulation Hide the data access modifier "private" , "protected" Polymorphism Redefine the function logic keyword "extends" and redefine the function in child class Abstraction Hide internal logic Abstract class & interface Both are used for keeping common structure to the child classes
Abstract class has shared properties and shared logics. But interface don't have properties and shared logics
2. Static and non static variablesStatic variable is single copy for all objects. but non-static variables are seperate copy for each objects
3. static methods vs non-static methodsin static method, member variables are not accessible. but in non-static method, member variables are accessible
MANVIA BLOG